// proxySubShopManage.js
const app = getApp()
var util = require("../../utils/util.js")

Page({

  /**
   * 页面的初始数据
   */
  data: {
    search: {
      searchAddShow: true,
      BtnTxt: '添加店铺',
      searchTip: '可输入ID、店名等',
      searchValue: '',
    },

    list: [],
    pagenum: 1
  },
  // 添加店铺
  addTap: function () {
    wx.navigateTo({
      url: '../../pages/proxySubShopAdd/proxySubShopAdd?submitUrl=agent/submitShop',
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.manageList('defalut');
    wx.stopPullDownRefresh()
  },
  searchChange: function (event) {
    this.setData({
      ['search.searchValue']: event.detail
    })
  },
  // 获取列表
  manageList: function (type) {
    var that = this;
    if (type == 'search') {
      that.setData({ list: [] })
    }
    var data = { page: that.data.pagenum, input: that.data.search.searchValue };
    app.post("agent/agentShop", data).then((res) => {
      if (res.code == 200) {
        
        var list = that.data.list;
        var dataArr = res.list;
        list = list.concat(dataArr)
        that.setData({
          list: list
        })
      }
    }).catch((errMsg) => {
      console.log(errMsg);//错误提示信息
    })
  },
  // 搜索
  onSearch: function () {
    this.manageList('search');
  },
  // 店铺详情
  ToDetailTap: function (e) {
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '../../pages/proxySubShopDetail/proxySubShopDetail?id='+id,
    })
  },
  // 套餐编辑
  ToPackageTap:function(e){
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '../../pages/proxySubShopPackage/proxySubShopPackage?id=' + id,
    })
  },
  // 设备列表
  ToDeviceTap: function (e) {
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '../../pages/proxySubShopDevice/proxySubShopDevice?id=' + id ,
    })
  },
  // 绑定商家等工作人员
  ToPartnerTap:function(e){
    var id = e.currentTarget.dataset.id;
    var commission = e.currentTarget.dataset.rate;
    wx.navigateTo({
      url: '../../pages/proxySubShopPartner/proxySubShopPartner?id=' + id + '&commission=' + commission,
    })
  },
  // 使用率分析
  UserateTap:function(e){
    var id = e.currentTarget.dataset.id;
    var that = this;
    wx.navigateTo({
      url: '../../pages/proxyUsageRateDetail/proxyUsageRateDetail?id=' + id + '&openid=' + app.globalData.openId + '&timeSelect=29&selectindex=0',
    })
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      pagenum: 1,
      list: [],
      ['search.searchValue']:''
    })
    this.onLoad()
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    var pagenum = that.data.pagenum + 1; //获取当前页数并+1
    that.setData({
      pagenum: pagenum, //更新当前页数
    })
    that.manageList();//重新调用请求获取下一页数据
  }
})